<?php
	
class Admin_CountryController extends TN_Controller_Action_Admin
{
 public function init()
    {
    	parent::init();
    }

 	public function indexAction()
 	{
    	$content = new Zend_Models_Country();
 		$this->view->contents = $content->getAll();
 	}
 	
 	public function newcountryAction()
 	{
	    $form = $this->addCountryForm();
 		$this->view->form = $form;	
        if (!$this->getRequest()->isPost())
     	{
     	  	return;
     	}
     	else
    	{
    		if (!$form->isValid($_POST))
      		{
				$this->view->form = $form;
				return;
			}
			else
			{
				$values = $form->getValues();
				$content = new Zend_Models_Country();
				$content = $content->createRow();
				$content->title = $values['common']['title'];
				$content->enabled = $values['common']['enabled'];
				$content->id_currency = $values['common']['currency'];
				$content->shipping_cost = $values['common']['shipping_cost'];
				$content->save();
				$this->_redirect("/admin/country/index");
			}
		}
	 }
	 
	private function addCountryForm()
	 {
		$form = new Zend_Form();
	 	$form->setAction("/admin/country/newcountry")
	             ->setMethod('post');
    	$form->setAttrib("class", "jNice");         
        /**** Datos comunes ****/
    	$common = new Zend_Form_SubForm(); 
    	$enabled = $common->createElement('checkbox','enabled');
 	    $enabled->setLabel('Activo:');
 	    $common->addElement($enabled);	
 	    $title = $common->createElement("text",'title');
		$title->setAttrib("class", "text-long");
 	   	$title->addValidator('NotEmpty')->setLabel($this->view->t->_('*Titulo:'))->setRequired(true);
		$title->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
		$common->addElement($title);
		
		$currency = $form->createElement("select","currency");
		$currencies = new Zend_Models_Currency();
		$currencies = $currencies->getAll();	
		$currency->setLabel('*Divisa:');
		$currency->addMultiOptions($this->formatCategories($currencies)); 
		$common->addElement($currency);
		
		$shipping = $common->createElement("text", "shipping_cost");
		$shipping->setAttrib("class", "text-long");
		$shipping->addValidator('Float')->setLabel('*Costo de envio:')->setRequired(true);
		$shipping->addErrorMessage('Valor real invalido');
 	    $common->addElement($shipping);
 	    
		
		$form->addSubForms(array("common" => $common));
	    /**** Fin datos comunes ***/
	    $form->addElement('submit', 'add', array('label' => 'Aceptar'));
		return $form;
	}
 	
	private function formatCategories($categories)
	{
		$options = array();
	
		foreach( $categories as $c )
		{
			$options[$c['id']] = $c['symbol'];
		}
		return $options;
	}
	
 	public function editcountryAction()
 	{
 		$form = $this->editCountryForm();
 		$this->view->form = $form;	
 		if (!$this->getRequest()->isPost())
    	{
       		return;
     	}
    	 else
    	 {
    		if (!$form->isValid($_POST))
    	  	{
				$this->view->form = $form;
				return;
			}
			else
			{
				$values = $form->getValues();
				$country = new Zend_Models_Country();
				$country = $country->getCountryById($values['id_country']);
				$country->enabled = $values['common']['enabled'];
				$country->title = $values['common']['title'];
				$country->id_currency = $values['common']['currency'];
				$country->shipping_cost = $values['common']['shipping_cost'];
				$country->save();
				$this->_redirect("/admin/country/index");
			}
		}
 	}
 	
	private function editCountryForm()
	{
 		$form = new Zend_Form();
 		$form->setAction("/admin/country/editcountry")
       	      ->setMethod('post');
    	$form->setAttrib("class", "jNice");   
    	$id_country = $this->_request->getParam('country');
    
    	$country = new Zend_Models_Country();  
    	$country = $country->getCountryById($id_country);         
         /**** Datos comunes ****/
    	$common = new Zend_Form_SubForm(); 
    	$enabled = $common->createElement('checkbox','enabled');
 	    $enabled->setLabel('Activo:');
 	    $enabled->setValue($country->enabled);
 	    $common->addElement($enabled);
 	    $title = $common->createElement("text","title");
		$title->setAttrib("class", "text-long");
 	   	$title->addValidator('NotEmpty')->setLabel($this->view->t->_('*Titulo:'))->setRequired(true);
		$title->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
		$title->setValue($country->title);
		$common->addElement($title);	
		
		$currency = $form->createElement("select","currency");
		$currencies = new Zend_Models_Currency();
		$currencies = $currencies->getAll();	
		$currency->setLabel('*Divisa:');
		$currency->addMultiOptions($this->formatCategories($currencies));
		$currency->setValue($country->id_currency); 
		$common->addElement($currency);
		
		$shipping = $common->createElement("text", "shipping_cost");
		$shipping->setAttrib("class", "text-long");
		$shipping->addValidator('Float')->setLabel('*Costo de envio:')->setRequired(true);
		$shipping->addErrorMessage('Valor real invalido');
		$shipping->setValue($country->shipping_cost);
 	    $common->addElement($shipping);
		
	    $form->addSubForms(array("common" => $common));
	    /**** Fin datos comunes ***/     
	 	$id = $form->createElement("hidden","id_country");
		$id->setValue($country->id);
    	$form->addElement($id);
     	$form->addElement('submit', 'add', array('label' => 'Aceptar'));
		return $form;
 }
 
 	public function deletecountryAction()
	{
 		$this->disableView();
 		$request = $this->_request->getParams();
    	$country = $request['country'];
   	 	$c = new Zend_Models_Country();
   	 	$c = $c->getCountryById($country);
    	$c->delete();
    	$this->_redirect("/admin/country/index");
 	}
}    